# -*- coding: utf-8 -*-
import pendulum
from airflow.exceptions import AirflowSkipException
from datetime import timedelta
from utils.operators.cluster_for_spark_sql_operator  import SparkSqlOperator

# from ai_ageing_cusc_hi.depend.dwd_depend import jms_dwd__dwd_tab_barscan_collect_base_dt
# from ai_ageing_cusc_hi.depend.dwd_depend import jms_dwd__dwd_tab_barscan_sign_base_dt
# from ai_ageing_cusc_hi.depend.ods_depend import jms_ods__yl_lmdm_sys_staff
# from ai_ageing_cusc_hi.depend.dwd_depend import jms_dwd__dwd_tab_barscan_taking_base_dt
# from ai_ageing_cusc_hi.depend.dwd_depend import jms_dwd__dwd_tab_barscan_sitearrival_base_dt
# from ai_ageing_cusc_hi.depend.dwd_depend import jms_dwd__dwd_tab_barscan_deliver_base_dt
# from ai_ageing_cusc_hi.depend.dwd_depend import jms_dwd__dwd_sqs_registration_problem_piece_base_dt
# from ai_ageing_cusc_hi.depend.dwd_depend import jms_dwd__dwd_work_order_dt
# from ai_ageing_cusc_hi.depend.dwd_depend import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt

cst = pendulum.timezone('Asia/Shanghai')
class NetworkRichSqlSensor(SparkSqlOperator):

    def pre_execute(self, context):
        day = cst.convert(context['ti'].execution_date) + timedelta(hours=6)

        schedule_date = ['06']

        if day.strftime('%H') not in schedule_date:
            print(f'{day.strftime("%H")} not in {schedule_date}, should skip')
            super().pre_execute(context)
            raise AirflowSkipException()
        else:
            print(f'{day.strftime("%H")} in {schedule_date}, run now')
            super().pre_execute(context)

jms_ai_group__complaints_dim_network_dt = NetworkRichSqlSensor(
    task_id='jms_ai_group__complaints_dim_network_dt',
    email=['jarl.huang@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    name='jms_ai_group__complaints_dim_network_dt_{{ execution_date | date_add(1) | cst_ds }}',
    email_on_retry=True,
    retries=1,
    sql='ai_ageing_cusc_hi/dm/complaints_dim_network_dt/execute.sql',
    yarn_queue='pro',
    driver_memory='4G',
    driver_cores=4,
    executor_memory='16G',
    executor_cores=2,
    pool_slots=2,
    pool='unlimited_pool',
    num_executors=30,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.executor.memoryOverhead': '5G',
          'spark.dynamicAllocation.enabled': 'true',
          'spark.shuffle.service.enabled': 'true',
          'spark.dynamicAllocation.maxExecutors': 50,
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',
          'spark.dynamicallocation.enabled': 'true',
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 120,
          'spark.sql.shuffle.partitions': 400,
          'spark.port.maxRetries': 100
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions.pernode': 5000,
              'hive.exec.max.dynamic.partitions': 30000
              },
    execution_timeout=timedelta(hours=2),
    priority_weight=20,
)  # type: SparkSqlOperator

# jms_ai_group__complaints_dim_network_dt << [
#     jms_dwd__dwd_tab_barscan_collect_base_dt,
#     jms_dwd__dwd_tab_barscan_sign_base_dt,
#     jms_ods__yl_lmdm_sys_staff,
#     jms_dwd__dwd_tab_barscan_taking_base_dt,
#     jms_dwd__dwd_tab_barscan_sitearrival_base_dt,
#     jms_dwd__dwd_tab_barscan_deliver_base_dt,
#     jms_dwd__dwd_yl_oms_oms_waybill_incre_dt,
#     jms_dwd__dwd_sqs_registration_problem_piece_base_dt,
#     jms_dwd__dwd_work_order_dt
#
# ]
